<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'FileShow.jsp' starting page</title>
  	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/easyui/1.3.4/themes/default/easyui.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/wu.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/icon.css" />
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/1.3.4/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/easyui/1.3.4/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript">
		$(function(){
			var msg = $("#msg").val();
			if(msg!=""){
				$.messager.show({
					title:'消息',
					msg:msg
				});
				$("#msg").val("");
			}
		
			$('#tt').tree({
	    		url:'${pageContext.request.contextPath}/file?op=showroot',
	    		onClick: function(node){
	    			$("input[name='dpid']").val(node.id);
	    			$('#lasturl').val($('#dateg').datagrid("options").url);
	    			$('#dateg').datagrid("options").url="${pageContext.request.contextPath}/file?op=showflist&pid="+node.id;
	    			$('#dateg').datagrid("reload");
	    		},
	    		//加载界面默认选中
	    		onLoadSuccess: function (node, data) {
				    if (data.length > 0) {
				          //找到第一个元素
				          var n = $('#tt').tree('find', data[0].id);
				          //调用选中事件
				          $('#tt').tree('select', n.target);
				    }
			      }
			});
			
				
			$('#starttime').datebox({   
			}); 
			$('#endtime').datebox({   
			});
			
			$('#dateg').datagrid({   
			    url:'${pageContext.request.contextPath}/file?op=showflist', 
			    fitColumns:true, 
			    striped:true, 
			    singleSelect:true,
			    columns:[[   
			        {field:'name',title:'名称',width:220},   
			        {field:'filetype',title:'类型',width:120,
				        formatter:function(value,row,index){
			        		return value.name;
			        	}
			        },   
			        {field:'user',title:'所有者',width:120,
			        	formatter:function(value,row,index){
			        		return value.realname;
			        	}
			        },  
			        {field:'createtime',title:'创建时间',width:170,
			        	formatter:function(value,row,index){
			        		return timeformat(value);
			        	}
			        },   
			        {field:'filetype2',title:'',width:18,
				        formatter:function(value,row,index){
			        		if(row.filetype.id!=-1){
			        			return "<a href='javascript:download()'><img src='${pageContext.request.contextPath}/css/icons/download.png'></img></a>"
			        		}
			        	}
			        } ,
			        {field:'power',title:"操作",width:120,align:'center',
			        	formatter:function(value,row,index){
			        		var userid = ${sys_user.id }
							if(userid==value.id){
								return "<a class='update' href = 'javaScript:updateFile()'>修改</a> <a class='delete' href='javaScript:deleteFile(0)'>删除</a>";
							}	
							return "";		        		
			        	}
			        } 
			    ]],
			    onDblClickRow:function(rowIndex, rowData){
					if(rowData.filetype.id==-1){
						$("input[name='dpid']").val(rowData.id);
						$('#lasturl').val($('#dateg').datagrid("options").url);
						$('#dateg').datagrid("options").url="${pageContext.request.contextPath}/file?op=showchild&pid="+rowData.id+"&typeid="+rowData.filetype.id;
  						$('#dateg').datagrid("reload");
						var node = $('#tt').tree('getSelected');
						var nodes = getSubChildren(node.id);
						for(var i=0;i<nodes.length;i++){
							var n = nodes[i];
							if(n.id==rowData.id){
								$('#tt').tree('select',n.target);
								return;
							}
						}
						var child = [{"id":rowData.id,"text":rowData.name,"iconCls":"icon-folder"}];
						$('#tt').tree('append', {parent:node.target,data:child});
					}else{
						var file = "<%= basePath%>文档管理"+rowData.path+rowData.name+"."+rowData.filetype.name;
						window.location.href = file;
					}
				} ,
			    toolbar : [
			    {
					iconCls : 'icon-back',
					text:'后退',
					handler : function() {
						var url = $('#lasturl').val();
						$('#dateg').datagrid("options").url=url;
						$('#dateg').datagrid("reload");
					}
				} 
			    ,"-",{
					iconCls : 'icon-folder',
					text:'添加文件夹',
					handler : function() {
						$('#addD').window({
							title:'添加文件夹',   
						    width:600,   
						    height:400,   
						    modal:true  
						});
					}
				}, {
					iconCls : 'icon-page',
					text:'添加文件',
					handler : function() {
						$('#cc').combobox({   
						    url:'${pageContext.request.contextPath}/file?op=gettype',   
						    valueField:'id',   
						    textField:'name'  
						});  
						$('#addF').window({
							title:'添加文件',   
						    width:600,   
						    height:400,   
						    modal:true  
						});
					}
				} ],
				onBeforeLoad:function(){
					//获取地址。
					var url =$('#dateg').datagrid("options").url;
					$.ajax({
					   type: "POST",
					   url: "${pageContext.request.contextPath}/file?op=givemepath",
					   data: 'urls='+url,
					   success: function(path){
					   		$("input[name='nowpath']").val(path);
					   		$(".dpath").text(path);
					   }
					});
				}
			});
			
		});
		//转换时间
		function add0(m){return m<10?'0'+m:m; };
		function timeformat(timestamp){
		  //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
			var time = new Date(timestamp);
			var year = time.getFullYear();
			var month = time.getMonth()+1;
			var date = time.getDate();
			var hours = time.getHours();
			var minutes = time.getMinutes();
			var seconds = time.getSeconds();
			return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds);
		}
		
		//获取某节点的一级节点
		function getSubChildren(id){
		    var $tree = $('#tt');
		    var node = $tree.tree('find',id);
		    var subNodes = [];
		    $(node.target)
		    .next().children().children("div.tree-node").each(function(){   
		        subNodes.push($tree.tree('getNode',this));
		    });
		   return subNodes;
		}
		
		//修改方法
		function updateFile(){
			var data = $('#dateg').datagrid('getSelected');
			$("#cname").val(data.name);
			$("#tip").val(data.tip);
			$("#path").text(data.path);
			$("input[name='path']").val(data.path);
			$("input[name='filetype.id']").val(data.filetype.id);
			$("#user").text(data.user.realname);
			$("#creattime").text(timeformat(data.createtime));
			$("input[name='id']").val(data.id);
			$('#upwin').window({
				title:'修改文件',  
			    width:600,
			    height:400,
			    modal:true
			});
		}
		//删除方法
		function deleteFile(){
			var data = $('#dateg').datagrid('getSelected');
			if(confirm("确认删除 "+data.name+" 吗？")){
				window.location.href ="${pageContext.request.contextPath}/file?op=changedelete&id="+data.id+"&deletestate=1";
			}
		}
		function download(){
			var rowData = $('#dateg').datagrid('getSelected');
			var file = "<%= basePath%>文档管理"+rowData.path+rowData.name+"."+rowData.filetype.name;
			window.location.href = file;
		}
	</script>
	<style type="text/css">
		.datagrid-toolbar table{
		    float: left;
		}
		#uptable{
			margin: 25px;
		}
		#uptable td{
			padding: 5px;
		}
		.addtable{
			margin: 25px;
		}
		.addtable td{
			padding: 5px;
		}
		body{
			margin: 0px;
			padding: 0px;
		}
	</style>
  </head>
  
<body class="easyui-layout">
	<input type="hidden" name="lasturl" id="lasturl" value="">
	<input type="hidden" name="msg" id="msg" value="${msg }">  
    <div data-options="region:'west',split:true" style="width:185px;">
    	<ul id="tt"></ul>
    </div>  
    <div data-options="region:'center'" style="padding:5px;">
   
    	<form action="" method="post">
    	 <!-- 
    		文件名：<input type="text" value="" name="name"/>
    		所有者：<input type="text" value="" name="realname"/>
    		创建日期：<input id="starttime" type="text"></input>-<input id="endtime" type="text"></input>
    		<input type="submit" value="检索">
    	 -->
    		<table id="dateg"></table>
    	</form>
    </div>  
    <div id="upwin">
    	<form action="${pageContext.request.contextPath}/file?op=doupdate" method="post">
    		<input type="hidden" name="id" value="">
	    	<table id="uptable">
	    		<tr>
	    			<td>文件夹名：</td>
	    			<td><input type="text" value="" id="cname" name="name"></td>
	    		</tr>
	    		<tr>
	    			<td>路径：</td>
	    			<td>
	    				<span id="path"></span>
	    				<input type="hidden" value="" name="path"/>
	    				<input type="hidden" value="" name="filetype.id"/>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td>创建人：</td>
	    			<td>
	    				<span id="user"></span>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td>创建时间：</td>
	    			<td>
	    				<span id="creattime"></span>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td>备注：</td>
	    			<td>
	    				<textarea name="tip" id="tip" rows="7" cols="35"></textarea>
	    			</td>
	    		</tr>
	    		<tr>
	    			<td></td>
	    			<td>
	    				<input type="submit" value="修改"/>
	    			</td>
	    		</tr>
	    	</table>
		</form>
		
    </div>
    <div id="addD">
    	<form action="${pageContext.request.contextPath}/file?op=addfloder" method="post">
	    	<table class="addtable">
	    		<tr>
					<td>文件夹名：</td>    
					<td>
						<input type="text" name="dname" value="">
					</td>
	    		</tr>
	    		<tr>
					<td>路径名：</td>    		
					<td>
						<span id="dpath" class = "dpath"></span>
						<input type="hidden" value="" name="nowpath"/>								
						<input type="hidden" value="1" name="dpid"/>								
					</td>
	    		</tr>
	    		<tr>
					<td>创建人：</td>    		
					<td>
						<span id="duser">${sys_user.realname }</span>
					</td>
	    		</tr>
	    		<tr>
					<td>备注：</td>    		
					<td>
						<textarea rows="5" cols="20" name="dtip"></textarea>
					</td>
	    		</tr>
	    		<tr>
	    			<td colspan="1"></td>
	    			<td colspan="1"><input type="submit" value="添加"> </td>
	    		</tr>
	    	</table>
    	</form>
    </div>  
    <div id="addF">
    	<form action="${pageContext.request.contextPath}/file?op=addfile" method="post" enctype="multipart/form-data"> 
	    	<table class="addtable">
	    		<tr>
					<td>文件名：</td>    
					<td>
						<input type="text" name="fname" value="">
					</td>
	    		</tr>
	    		<tr>
					<td>路径名：</td>    		
					<td>
						<span id="dpath" class="dpath"></span>
						<input type="hidden" value="" name="nowpath"/>								
						<input type="hidden" value="1" name="dpid"/>								
					</td>
	    		</tr>
	    		<tr>
					<td>创建人：</td>    		
					<td>
						<span id="fuser">${sys_user.realname }</span>
					</td>
	    		</tr>
	    		<tr>
	    			<td>文件类型：</td>
	    			<td>
	    				<input id="cc" name="filetype" value="1">  
	    			</td>
	    		</tr>
	    		<tr>
	    			<td>文件上传：</td>
	    			<td><input type="file" name="file" /></td>
	    		</tr>
	    		<tr>
					<td>备注：</td>    		
					<td>
						<textarea rows="5" cols="20" name="ftip"></textarea>
					</td>
	    		</tr>
	    		<tr>
	    			<td colspan="1"></td>
	    			<td colspan="1"><input type="submit" value="添加"> </td>
	    		</tr>
	    	</table>
    	</form>
    </div>  
</body>
</html>
